package com.kanche.bigbaby.collection;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.domain.VehicleModel;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

//import com.kkche.merchant.R;
//import com.kkche.merchant.domain.VehicleModel;

/**
 * Created by nealmi on 6/12/14.
 */
public class VehicleSelectorAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer {

    private List<VehicleModel> mList;
    private Context mContext;
    private int type;
    private Map<String, Integer> map = new HashMap<String, Integer>();
    private Map<Integer, Integer> indexSections = new HashMap<Integer, Integer>();
    private Set<String> set = new LinkedHashSet<String>();
    private String[] sections;

    public VehicleSelectorAdapter(List<VehicleModel> list, int type, Context context) {
        this.type = type;
        this.mContext = context;
        this.mList = list;
        getSections();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int i) {
        return mList.get(i).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        Holder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);

            convertView = inflater.inflate(R.layout.vehicle_model_list_item, null);
            holder = new Holder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            //holder.subtitle = (TextView) convertView.findViewById(R.id.subtitle);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        VehicleModel vehicleModel = (VehicleModel) getItem(position);
        String text = "";
        switch (type) {
            case VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND:
                text = vehicleModel.getBrand();
                break;
            case VehicleModelSelectorFragment.VEHICLE_TYPE_SERIES:
                text = vehicleModel.getSeries();
                break;
            case VehicleModelSelectorFragment.VEHICLE_TYPE_MODEL:
                text = vehicleModel.getSaleName();
                break;

        }
        holder.title.setText(text);
        return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);

            convertView = inflater.inflate(R.layout.list_section_header, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        String headerText = "";
        VehicleModel model = mList.get(position);
        if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND && model.getId() == VehicleModel.HOT_ID) {
            headerText = "热门品牌";
        } else if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND)
            headerText = "" + StringUtils.getFirstLetterOfPinyin(model.getBrand());
        else if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_SERIES)
            headerText = model.getMaker();
        else if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_MODEL)
            headerText = model.getYearGroup();

        if (model.getId() == VehicleModel.UNLIMITED_ID) {
            headerText = "#";
//            headerText = "*";
        }
        holder.text.setText(headerText);
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        VehicleModel model = mList.get(position);
        long headerId = 0L;
        if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND && model.getId() == VehicleModel.HOT_ID) {
            headerId = VehicleModel.HOT_ID;
        } else if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND)
            headerId = StringUtils.getFirstLetterOfPinyin(model.getBrand());
        else if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_SERIES)
            headerId = model.getMaker().hashCode();
        else if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_MODEL)
            headerId = model.getYearGroup().hashCode();

        return headerId;
    }

    public void refresh(List<VehicleModel> vehicleModels) {
        this.mList = vehicleModels;
        notifyDataSetChanged();
    }

    @Override
    public Object[] getSections() {
        int len = mList.size();
        for (int i = 0; i < len; i++) {
            VehicleModel model = mList.get(i);
            String section = "";
            if (model.getId() == VehicleModel.UNLIMITED_ID) {
                continue;
            }
            if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND) {
                section = (model.getId() == VehicleModel.HOT_ID) ? "#" : StringUtils.getFirstPinyinLetterAsString(model.getBrand());
            }
//                if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_BRAND)
//                    section = StringUtils.getFirstPinyinLetterAsString(model.getBrand());

            if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_SERIES) {
                continue;
//                section = StringUtils.getFirstPinyinLetterAsString(model.getMaker());
            }

            if (type == VehicleModelSelectorFragment.VEHICLE_TYPE_MODEL)
                  continue;
//                section = StringUtils.getFirstPinyinLetterAsString(model.getYearGroup());


            if (!set.contains(section)) {
                map.put(section, i);
                set.add(section);
            }
            indexSections.put(i, set.size() - 1);

        }
        sections = set.toArray(new String[]{});
        Arrays.sort(sections);
        return sections;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return map.get(sections[sectionIndex]);
    }

    @Override
    public int getSectionForPosition(int position) {
        return indexSections.get(position);
    }

    public class HeaderViewHolder {
        private TextView text;

    }

    class Holder {
        private TextView title;
    }
}
